package com.kh.statistics.video.core.script;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Logger;

public class StreamGobbler extends Thread {
    private static final Logger LOGGER = Logger.getLogger(StreamGobbler.class.getName());
    private final InputStream inputStream;
    private final List<String> lines = new ArrayList<>();
    private final String type;

    public StreamGobbler(InputStream inputStream, String type) {
        this.inputStream = inputStream;
        this.type = type;
    }

    @Override
    public void run() {
        try (BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream))) {
            String line;
            while ((line = reader.readLine()) != null) {
                lines.add(line);
                LOGGER.info(type + ": " + line);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public List<String> getLines() {
        return lines;
    }
}
